<template>
  <div class="employees-container">
    <div class="app-container">
      <PageTabs>
        <template #left>
          <span>总记录数: {{ total }}条</span>
        </template>
        <template #right>
          <el-button
            type="warning"
            size="small"
            @click="$router.push('/import')"
            >excel导入</el-button
          >
          <el-button type="danger" size="small" @click="doExport"
            >excel导出</el-button
          >
          <el-button type="primary" size="small" @click="showAddDialog"
            >新增员工</el-button
          >
        </template>
      </PageTabs>

      <el-card style="margin-top: 10px">
        <el-table
          border
          :data="employeesList"
          :default-sort="{ prop: 'workNumber' }"
        >
          <el-table-column label="序号" type="index" width="50" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" width="125">
            <template v-slot="scope">
              <!-- <img :src="scope.row.staffPhoto" alt="" /> -->
              <ImageHolder :src="scope.row.staffPhoto" />
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" sortable />
          <el-table-column label="聘用形式">
            <template v-slot="scope">
              {{ formatType(scope.row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry" sortable />
          <el-table-column label="操作" prop="" width="150">
            <template v-slot="scope">
              <el-button
                type="text"
                size="small"
                @click="$router.push('/employees/detail?id=' + scope.row.id)"
                >查看</el-button
              >
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small" @click="hdel(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            layout="sizes, prev, pager, next"
            :page-sizes="[2, 5, 10, 20, 50, 100]"
            :total="total"
            :page-size="pageParams.size"
            :current-page="pageParams.page"
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          />
        </el-row>
      </el-card>
    </div>

    <el-dialog :visible.sync="showDialog" title="新增员工">
      <empDialog @doCancel="doCancel" @doSuccess="doSuccess"></empDialog>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployees, delEmployees } from "@/api/employees";
import empDialog from "./empDialog.vue";
import { formatType, formatExport } from "@/utils/index";

export default {
  components: { empDialog },
  data() {
    return {
      total: 1,
      pageParams: {
        page: 1,
        size: 2,
      },
      employeesList: [],
      hireType: {},
      showDialog: false,
    };
  },
  created() {
    this.loadEmployees();
  },
  methods: {
    formatType(id) {
      return formatType(id);
    },
    async loadEmployees() {
      const res = await getEmployees(this.pageParams);
      console.log(res);
      this.total = res.data.total;
      this.employeesList = res.data.rows;
    },
    hCurrentChange(curPage) {
      this.pageParams.page = curPage;
      this.loadEmployees();
    },
    hSizeChange(curSize) {
      this.pageParams.size = curSize;
      this.loadEmployees();
    },
    hdel(id) {
      this.$confirm("此操作将会永久删除数据,是否继续", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除成功
          this.dodel(id);
        })
        .catch(() => {
          // 取消删除
          this.$message.info("已取消删除");
        });
    },
    async dodel(id) {
      try {
        if (this.employeesList.length === 1) {
          this.pageParams.page--;
          this.pageParams.page = Math.max(1, this.pageParams.page);
        }
        const res = await delEmployees(id);
        console.log(res);
        this.$message.success(res.message);
        this.loadEmployees();
      } catch (err) {
        this.$message.error(err.message);
      }
    },
    showAddDialog() {
      this.showDialog = true;
    },
    doCancel() {
      this.showDialog = false;
    },
    doSuccess() {
      this.total++;
      this.pageParams.page = Math.ceil(this.total / this.pageParams.size);
      this.loadEmployees();
      this.showDialog = false;
    },
    doExport() {
      import("@/vendor/Export2Excel").then(async (excel) => {
        // excel表示导入的模块对象
        console.log(excel);
        const res = await getEmployees(this.pageParams);
        console.log(res);
        const data = formatExport(res.data.rows);
        console.log(data);
        excel.export_json_to_excel({
          header: data.zhHeader, // 表头 必填
          data: data.data, // 具体数据 必填
          filename: "员工基本信息表", // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: "xlsx", // 生成的文件类型
        });
      });
    },
  },
};
</script>
